package xyz.xpman.prisonerfitness.entity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.util.Log;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;


@SuppressWarnings("unused")
public class SqlLiteModel {

    // condition attrs
    protected final ContentValues attrs = new ContentValues();

    /**
     * where 条件顺序
     * 更新字段value时，不改变字段条件顺序
     */
    protected final LinkedHashSet<String> condSer = new LinkedHashSet<>();




    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    private void addCondAttr(String key) {
        condSer.add(key);
    }

    private void removeCondAttr(String key) {
        condSer.remove(key);
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    protected void setObject(String key, Object value) {
        if (value == null) {
            setNull(key);
        } else if (value instanceof String) {
            set(key, (String) value);
        } else if (value instanceof Byte) {
            set(key, (Byte) value);
        } else if (value instanceof Short) {
            set(key, (Short) value);
        } else if (value instanceof Integer) {
            set(key, (Integer) value);
        } else if (value instanceof Long) {
            set(key, (Long) value);
        } else if (value instanceof Float) {
            set(key, (Float) value);
        } else if (value instanceof Double) {
            set(key, (Double) value);
        } else if (value instanceof Boolean) {
            set(key, (Boolean) value);
        } else if (value instanceof byte[]) {
            set(key, (byte[]) value);
        } else {
            Log.i("SqlLiteModel.putObject", "Unsupported type: " + value.getClass());
            // throw new IllegalArgumentException("Unsupported type " + value.getClass());
        }
    }


    // set method proxy for ContentValues
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -





    public void set(String key, String value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void setAll(Condition other) {
        attrs.putAll(other.attrs);
    }




    public void set(String key, Byte value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Short value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Integer value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Long value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Float value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Double value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, Boolean value) {
        attrs.put(key, value);
        addCondAttr(key);
    }

    public void set(String key, byte[] value) {
        attrs.put(key, value);
        addCondAttr(key);
    }



    public void setNull(String key) {
        attrs.putNull(key);
        addCondAttr(key);
    }




    // get method proxy for ContentValues
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public Object get(String key) {
        return attrs.get(key);
    }

    public String getString(String key) {
        return attrs.getAsString(key);
    }

    public Long getLong(String key) {
        return attrs.getAsLong(key);
    }

    public long getLong(String key, long defaultValue) {
        Long value = attrs.getAsLong(key);
        return null == value ? defaultValue : value;
    }

    public Integer getInteger(String key) {
        return attrs.getAsInteger(key);
    }

    public int getInteger(String key, int defaultValue) {
        return getInt(key, defaultValue);
    }

    public int getInt(String key, int defaultValue) {
        Integer value = attrs.getAsInteger(key);
        return null == value ? defaultValue : value;
    }

    public Short getShort(String key) {
        return attrs.getAsShort(key);
    }

    public short getShort(String key, short defaultValue) {
        Short value = attrs.getAsShort(key);
        return null == value ? defaultValue : value;
    }

    public Byte getByte(String key) {
        return attrs.getAsByte(key);
    }

    public Double getDouble(String key) {
        return attrs.getAsDouble(key);
    }

    public double getDouble(String key, double defaultValue) {
        Double value = attrs.getAsDouble(key);
        return null == value ? defaultValue : value;
    }

    public Float getFloat(String key) {
        return attrs.getAsFloat(key);
    }

    public float getFloat(String key, float defaultValue) {
        Float value = attrs.getAsFloat(key);
        return null == value ? defaultValue : value;
    }

    public Boolean getBoolean(String key) {
        return attrs.getAsBoolean(key);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        Boolean value = attrs.getAsBoolean(key);
        return null == value ? defaultValue : value;
    }

    public byte[] getByteArray(String key) {
        return attrs.getAsByteArray(key);
    }


    // 杂项 method proxy for ContentValues
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public boolean containsKey(String key) {
        return attrs.containsKey(key);
    }

    public void remove(String key) {
        attrs.remove(key);
        removeCondAttr(key);
    }

    public Set<String> keySet() {
        return attrs.keySet();
    }

    public Set<Map.Entry<String, Object>> valueSet() {
        return attrs.valueSet();
    }

    public int size() {
        return attrs.size();
    }

    @SuppressLint("NewApi")
    public boolean isEmpty() {
        return attrs.isEmpty();
    }

}
